package com.hyhy.hycloud.uaa.security;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * 安全用户信息，实现UserDetails接口，会持久化到session里
 *
 * @author Xuegui Yuan
 * @date 2018-09-19
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true, value = {"password"})
public class SecurityUser implements UserDetails, CredentialsContainer {
    /**
     * 用户ID
     */
    private String id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 手机号
     */
    private String mobile;

    /**
     * 密码
     */
    private String password;

    /**
     * 权限列表
     */
    private Collection<? extends GrantedAuthority> authorities;

    /**
     * 会话过期时间, 默认值 0-用户未进行登出设置
     */
    private int maxInactiveInterval;

    private boolean active;

    public SecurityUser(String id, String username, String mobile, String password,
                        int maxInactiveInterval) {
        this.id = id;
        this.username = username;
        this.mobile = mobile;
        this.password = password;
        this.maxInactiveInterval = maxInactiveInterval;
    }

    public SecurityUser(String username, String mobile, String password) {
        this(null, username, mobile, password, 0);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return active;
    }

    @Override
    public boolean isAccountNonLocked() {
        return active;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return active;
    }

    @Override
    public boolean isEnabled() {
        return active;
    }

    @Override
    public void eraseCredentials() {
        this.password = null;
    }
}
